//
// Created by zhaog on 30/03/17.
//

#include "http_svr.h"
#include "lastest_img_req_handler.h"
#include "Poco/URI.h"
#include "general_img_req_handler.h"
#define GET_RECENT_IMG "/get_recent_img"
#define GET_GENERAL_IMG "/get_general_img"
namespace img_svr{
    SharedPtr<ImgReqHandlerFactory> req_factory;
    Poco::AutoPtr<Poco::Net::HTTPServerParams> http_params;
    HttpSvr::HttpSvr(uint16_t port, int thread_count) {
        req_factory=new ImgReqHandlerFactory;
        http_params=new HTTPServerParams();
        http_params->setMaxThreads(thread_count);
        _http_server=new Poco::Net::HTTPServer(req_factory,port,http_params);
    }

    void HttpSvr::start() {
        _http_server->start();
    }
    Poco::Net::HTTPRequestHandler* ImgReqHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &request) {
        Poco::URI uri(request.getURI());
        std::string req_path=uri.getPath();
        if(req_path==GET_RECENT_IMG){
            return new RecentImgReqHandler;
        }else if(req_path==GET_GENERAL_IMG){

            return new GeneralImgReqHandler;
        }
        else{
            return NULL;
        }
    }
}
